package dao;

import java.util.List;

import javax.persistence.EntityExistsException;
import javax.persistence.Query;
import javax.persistence.TransactionRequiredException;

import excepciones.DaoException;
import modelo.Actividad;
import modelo.AreaTematica;
import modelo.Model;
import modelo.Proyecto;

public class AreaTematicaDao extends AbstractDao {
	
	
	/**
	* Class Constructor [Empty]
	*/
	public AreaTematicaDao()
	{
	   super();
	}
	
	/**
    * Class Constructor
    * @param model   The Usuario model to manage persistence for
    */
    public AreaTematicaDao(AreaTematica AreaTematicaModel)
    {
      super(AreaTematicaModel);      
      this.model = (AreaTematica) model;
    }

	@Override
	public Model findById(Long id) throws DaoException {
		// TODO Auto-generated method stub
		try
	      {
	         model = (AreaTematica) super.findById(AreaTematica.class, id);
	      }
	      catch(Exception e)
	      {
	         e.printStackTrace();
	      }
	      
	      
	      return (AreaTematica) model;
	}

	@Override
	public Model getModel() {
		// TODO Auto-generated method stub
		return (AreaTematica) model;
	}
	
	public void createAccess() throws DaoException
	   {
	      startTxn();
	      try
	      {
	         entityManager.persist( model );
	         entityManager.getTransaction().commit();
	      }
	      catch(EntityExistsException e)
	      {
	         entityManager.getTransaction().rollback();
	         e.printStackTrace();
	         throw new DaoException("Create Failed: " + e.getMessage());
	      }
	      catch(IllegalArgumentException e)
	      {
	         entityManager.getTransaction().rollback();
	         e.printStackTrace();
	         throw new DaoException("Create Failed: " + e.getMessage());
	      }
	      catch(TransactionRequiredException e)
	      {
	         entityManager.getTransaction().rollback();
	         e.printStackTrace();
	         throw new DaoException("Create Failed: " + e.getMessage());
	      }
	   }

	public List<AreaTematica> getTodasAreas() throws DaoException {
		try
	    {				         
	       List <AreaTematica> areasTematicas = 
	      		 (List<AreaTematica>) super.getEntityManager().
	             createQuery("Select a from AreaTematica as a")              
	              .getResultList();
	       //super.setModel( UsuarioModel );
	       return areasTematicas;	
	    }
	    catch(IllegalStateException e)
	    {
	       throw new DaoException("Find by Email Failed: " + e.getMessage());
	    }
	    catch(IllegalArgumentException e)
	    {
	       throw new DaoException("Find by Email Failed: " + e.getMessage());
	    }
	    catch(Exception e)
	    {
	       throw new DaoException("Find by Email Failed: " + e.getMessage());
	    }
	}

	public AreaTematica encontrarPorNombre(String str) throws DaoException {
		
		try
	      {
	         model = (AreaTematica) super.getEntityManager().
	               createQuery("from AreaTematica as a where a.nombre= ?1")
	                .setParameter(1, str)
	                .getSingleResult();
	         //super.setModel( PersonaModel );
	         
	      }
	      catch(IllegalStateException e)
	      {
	         throw new DaoException("Find by Email Failed: " + e.getMessage());
	      }
	      catch(IllegalArgumentException e)
	      {
	         throw new DaoException("Find by Email Failed: " + e.getMessage());
	      }
	      catch(Exception e)
	      {
	         throw new DaoException("Find by Email Failed: " + e.getMessage());
	      }
	      	      
	      return (AreaTematica) model;
	   }	
	
	public List<AreaTematica> todasLasAreasTematicas() {
		// TODO Auto-generated method stub
		
		 Query cons = entityManager.createQuery("SELECT a from AreaTematica a");
		 List<AreaTematica> areas = (List<AreaTematica>) cons.getResultList();
		 
		return areas;
	}

}
